<?
function get_version_info(){
	$version["sshd"]="sshd -v";
	$version["apache"]="apache -v";
	$version["mysqld"]="mysqld --version";
}



class i18n {

	var $lang;

	function i18n($target_lang){
		include("lang/i18n_de.php");
		$this->lang=$lang[$target_lang];
	}

	function getString($string)
	{

		/*
			Try to find $string in $target_lang, if not return english
			version
		*/

		if(isset($this->lang[$string]))
		{
			return $this->lang[$string];
		} else {
			return $string;
		}
	}


	function getLanguages()
	{
		$lang_array=array();
		$lang_array["de"]="";
		$lang_array["en"]="";
		return $lang_array;
	}
}

class linux_sysinfo {


	function getServerName(){
		return $_SERVER["SERVER_NAME"];
	}

	function getCronHourly()
	{

		/*
			Open cron.hourly and read entry's
		*/


		$dir = "/etc/cron.hourly/";

		$crontab_lines=array();

		if (is_dir($dir)) {
			if ($dh = opendir($dir)) {
				while (($file = readdir($dh)) !== false) {
					if($file!="." && $file!="..")
						array_push($crontab_lines,$file);
				}
				closedir($dh);
			}
		}

		return $crontab_lines;
	}


	function getCronDaily()
	{
		/*
			Open cron.hourly and read entry's
		*/


		$dir = "/etc/cron.daily/";

		$crontab_lines=array();

		if (is_dir($dir)) {
			if ($dh = opendir($dir)) {
				while (($file = readdir($dh)) !== false) {
					if($file!="." && $file!="..")
						array_push($crontab_lines,$file);
				}
				closedir($dh);
			}
		}

		return $crontab_lines;


	}

	function getCronWeekly()
	{
		/*
			Open cron.weekly and read entry's
		*/


		$dir = "/etc/cron.weekly/";

		$crontab_lines=array();

		if (is_dir($dir)) {
			if ($dh = opendir($dir)) {
				while (($file = readdir($dh)) !== false) {
					if($file!="." && $file!="..")
						array_push($crontab_lines,$file);
				}
				closedir($dh);
			}
		}

		return $crontab_lines;

	}

	function getCronMonthly()
	{
		/*
			Open cron.hourly and read entry's
		*/


		$dir = "/etc/cron.monthly/";

		$crontab_lines=array();

		if (is_dir($dir)) {
			if ($dh = opendir($dir)) {
				while (($file = readdir($dh)) !== false) {
					if($file!="." && $file!="..")
						array_push($crontab_lines,$file);
				}
				closedir($dh);
			}
		}

		return $crontab_lines;
	}



	function getGlobalCrontab()
	{
		$crontab_lines=array();



		$lines=file("/etc/crontab");

		foreach($lines as $line)
		{
			$line=preg_replace("/^([^0-9\\*]).*/","",$line);
		if(trim($line)!=""){
				array_push($crontab_lines,$line);
			}
		}
		return $crontab_lines;
	}

	function getMountedFilesystems()
	{
		$handle=popen('/bin/mount','r');
		while (!feof($handle))
		{
			$fs .= fread($handle,1096);
		}
		pclose($handle);

		$lines=split("\n",$fs);

		#$filesystem["/dev/hda1"]
		$filesystems=array();

		foreach($lines as $line)
		{
			if(preg_match("/^\/dev\/.*/",$line))
			{
				$line=preg_replace("/ on /"," ",$line);
				$line=preg_replace("/ type /"," ",$line);
				$atom=split(" ",$line);


				$filesystems[$atom[0]]["mountpoint"]=$atom[1];
				$filesystems[$atom[0]]["type"]=$atom[2];
				$filesystems[$atom[0]]["options"]=$atom[3];
				$filesystems[$atom[0]]["freespace"]=intval(disk_free_space($atom[1])/1024/1024);
			}
		}

		return $filesystems;
	}

	function getPciDevices()
	{
		$handle=popen('/usr/bin/lspci','r');


		$pci_array=array();

		$mem="";

		while (!feof($handle))
		{
			$mem .= fread($handle,1096);
		}
		pclose($handle);


		$lines=split("\n",$mem);

		foreach($lines as $line)
		{
			$line=preg_replace("/^[0-9][0-9][0-9][0-9]\\:[0-9][0-9]\\:[0-9]([0-9a-z])\\.[0-9]/","",$line);
			if(trim($line)!="")
			{
				array_push($pci_array,$line);
			}
		}
		return $pci_array;
	}

	function getUsbDevices()
	{
		$handle=popen('/usr/bin/lsusb','r');

		$usb_array=array();

		$mem="";

		while (!feof($handle))
		{
			$mem .= fread($handle,1096);
		}
		pclose($handle);


		$lines=split("\n",$mem);

		foreach($lines as $line)
		{
			$line=preg_replace("/^bus [0-9][0-9][0-9] device [0-9][0-9][0-9]\\: id [0-9a-z][0-9a-z][0-9a-z][0-9a-z]\\:[0-9a-z][0-9a-z][0-9a-z][0-9a-z]/i","",$line); #.[0-9][0-9]
			if(strlen(trim($line))>1){
				array_push($usb_array,$line);
			}
		}

		return $usb_array;
	}

	function getSystemUsers()
	{
		$users=array();
		$a=file("/etc/passwd");
		foreach($a as $b)
		{
			$c=split(":",$b);
			$user["login"]=$c[0];
			$user["password"]=$c[1];
			$user["uid"]=$c[2];
			$user["gid"]=$c[3];
			$user["gcos"]=$c[4];
			$user["home"]=$c[5];
			$user["shell"]=$c[6];
			array_push($users,$user);
		}
		return $users;
	}

	function getRunlevel()
	{

		//runlevel[0]['nfs']['start']=0;
		//runlevel[0]['nfs']['kill']=1;

		$run=array("0","1","2","3","4","5","6","S");

		foreach($run as $level)
		{

			$dir = "/etc/rc$level.d/";
			if (is_dir($dir))
			{
				if ($dh = opendir($dir))
				{
					while (($file = readdir($dh)) !== false)
					{
						if($file!="." && $file!=".." && is_link($dir.$file)){
							$service=basename($file);
							$service=preg_replace("/^[A-Z][0-9][0-9]/","",$service);
							if($file[0]=="S")
								$runlevel[$service][$level]['start']="1";
							else
								$runlevel[$service][$level]['start']="0";

							if($file[0]=="K")
								$runlevel[$service][$level]['kill']="1";
							else
								$runlevel[$service][$level]['kill']="0";

							#print readlink($dir.$file)."<br>";
						}
					}
					closedir($dh);
				}
			}
		}
		return $runlevel;
	}

	function getProcesses()
	{

		$handle = popen('/bin/ps -aAo "%u %a" --no-heading', 'r');

		while (!feof($handle))
		{
			$read .= fread($handle,2096);
		}
		pclose($handle);

		$line=split("\n",$read);

		#delete out our own ps process
		array_pop($line);
		array_pop($line);

		$procs=array();

		foreach($line as $val){
			$arr=split(" ",$val,2);
			array_push($procs,$arr);
		}
		return $procs;
	}

	function getServerAddress()
	{
		return gethostbyname($this->getServerName());
	}

	function getKernelVersion()
	{
		//Kernelversion
		$handle = popen('/bin/uname -r', 'r');
		$kernel = fread($handle,1096);
		pclose($handle);
		return $kernel;
	}

	function getUptime()
	{
		$handle = popen('/bin/cat /proc/uptime', 'r');
		$uptime = fread($handle,1096);
		pclose($handle);
		$uptime = split(" ",$uptime);
		$uptime = floor($uptime[0]/3600);
		return $uptime;
	}

	function getActiveUsers()
	{
				//parse output of 'who -c' to get the logged_in_users
		$handle = popen('/usr/bin/who -q', 'r');
		$who = fread($handle,1096);
		pclose($handle);


		$logged_in_users=split("\n",$who);
		//$logged_in_users=$logged_in_users[1];
		$logged_in_users=preg_replace("/^# [a-zA-Z]*=/","",$logged_in_users[1]);
		return $logged_in_users;
	}

	function getOS()
	{
		$list = parse_ini_file("./distros.ini", true);
		if (!$list)
		{
			return;
		}

		foreach ($list as $section => $distribution) {
			if (!isset($distribution["Files"])) {
				continue;
			} else {
					foreach (explode(";", $distribution["Files"]) as $filename) {
						if (file_exists($filename)) {
							$distro=$distribution["Name"];
								break 2;
						}
					}
				}
		}
		return $distro;
	}

	function getMemoryUsage()
	{
		//get used ram
		$handle=popen('cat /proc/meminfo | grep "MemTotal"','r');
		$mem_total = fread($handle,1096);
		pclose($handle);

		$total_mem=preg_replace("/[^0-9]/","",$mem_total);

		$ram['total']=$total_mem;

		$handle=popen('cat /proc/meminfo | grep "MemFree"','r');
		$mem_free = fread($handle,1096);
		pclose($handle);

		$free_mem=preg_replace("/[^0-9]/","",$mem_free);

		$ram['free']=$free_mem;
		return $ram;

	}
}






?>